<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>

<?php	

	include("inc/joomlaInt.php");
	include("inc/inc_configuration.php");
	
if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{	
	$debug=0;
	
	// $tTid=$_POST["team"];
	
	$angebotId = $_POST["angebotId"]; 
	
	$cneu = $_POST["cId"]; // angebotene
	
	$q = "SELECT combinationId FROM $tbl_ttb WHERE id = $angebotId";
	$qe = mysql_query($q);
	$qeo = mysql_fetch_object($qe);
	
	$calt = $qeo->combinationId;
	
	$q = "SELECT c.userID, c.teamId, t.name FROM $tbl_combinations c, $tbl_teams t WHERE t.id=c.teamId AND c.id = $cneu";
	$q2 = "SELECT c.userID, c.teamId, t.name FROM $tbl_combinations c, $tbl_teams t WHERE t.id=c.teamId AND c.id = $calt";
	
	echo "Q:$q";
	$qe = mysql_query($q);
	$qeo = mysql_fetch_object($qe);
	$uneu = $qeo->userID;
	$tneu = $qeo->teamId;
	$tneun = $qeo->name;
	
	$qe = mysql_query($q2);
	$qeo = mysql_fetch_object($qe);
	$ualt = $qeo->userID;
	$talt = $qeo->teamId;
	$taltn = $qeo->name;
	
	$tTid = $tneu;
	
	if ($tTid == "")
	{
		echo "Bitte ein Team auswählen!";
		break;
	}
	$anz=$_POST["anz"];
	
	$q = "SELECT t.name,t.rating,lt.leagueId, t.id AS id FROM $tbl_lt lt, $tbl_teams t WHERE lt.teamId=t.id AND t.id=$tTid";
	$qe = mysql_query($q);
	$qeo = mysql_fetch_object($qe);
	
	
	$tTr = $qeo->rating;
	$tTl = $qeo->leagueId;
	$tTn = $qeo->name;
	$tTid = $qeo->id;
	
	
	$af_t_vh="	SELECT l.Id AS leagueId,  t.id AS teamId, t.rating AS rating, t.name AS name, c.id AS cId  
			FROM $tbl_combinations c, $tbl_teams t, $tbl_leagues l, $tbl_season s, $tbl_lt lt 
			WHERE 	c.userID=$jUser AND 
					c.active=1 AND 
					c.teamId=t.id AND
					s.active=1 AND
					l.seasonId=s.id AND
					lt.teamId=t.id AND
					lt.leagueId=l.id AND
					l.type=1"; //vorhandene Teams suchen
	$e_t_vh = mysql_query($af_t_vh);
	$count = mySQL_num_rows($e_t_vh);
	
	
	
	
	
	if ($count>=1)
	{
		$o=mysql_fetch_object($e_t_vh);
		$t1id=$o->teamId;
		$t1r=$o->rating;
		$t1l=$o->leagueId;
		$t1n=$o->name;
		$t1c=$o->cId;
	}
	if ($count==2)
	{
		$o=mysql_fetch_object($e_t_vh);
		$t2id=$o->teamId;
		$t2r=$o->rating;
		$t2l=$o->leagueId;
		$t2n=$o->name;
		$t2c=$o->cId;
	}
	
	// echo "C: $count";
	
	if ($count==1)
	{
		$p=1;
		$tabid = $t1id;
	}
	else
	{
			
			$t1la=0;
			$t2la=0;
			$t1ra=0;
			$t2ra=0;
			$t1a=0;
			$t2a=0;
			
			if ($t1id==$talt)
			{
				$t1a=1;
			}
			if ($t2id==$talt)
			{
				$t2a=1;
			}
			
			if ($tTl==$t1l)
			{
				$t1la=1;
			}
			
			if ($tTl==$t2l)
			{
				$t2la=1;
			}
			
			if (($maxrating-$t1r)<$tTr)
			{
				$t1ra=1;
			}
			if (($maxrating-$t2r)<$tTr)
			{
				$t2ra=1;
			}
			
			
			
			if (($t1la==1) OR ($t1ra==1))
			{
				$t1a=1;
			}
			
			if (($t2la==1) OR ($t2ra==1))
			{
				$t2a=1;
			}
			
			
			
			$p=3;
			
			$tabid=1234;
			
			if ($t1a==1)
			{
				$p=1;	//Team 1 muss abgegeben werden, es sei denn..
				
				$tabid = $t1id;
				if ($t2a==1)
				{
					$p=2;	//Team 1 & 2 müssen abgegeben werden
					$tab2id = $t2id;
				}
			}
			else
			{
				if ($t2a==1)
				{
					$p=1; //Team 2 muss abgegeben werden
					$tabid = $t2id;
				}
			}
			

				$deb="";
				$deb.= "HALLO: $p";
				$deb.= "Team1: $t1n, $t1r Sterne, Liga: $t1l - Team2: $t2n, $t2r Sterne, Liga: $t2l<br><br><br>Tauschteam: ID:$tTid $tTr Sterne, Liga: $tTl";
				$deb.= "<br><br>t1r:$t1ra<br>t1l:$t1la<br>t1a:$t1a<br><br>t2r:$t2ra<br>t2l:$t2la<br>t2a:$t2a<br><br>";

	}
	
	$qs = "SELECT * FROM $tbl_season s WHERE s.active=1";
	$qse = mysql_query($qs);
	$qseo = mysql_fetch_object($qse);
	$season = $qseo->id;
	
	$datetime = time();
	
	$i=0;
	
	switch($p)
	{
		case (1): //Team1 muss abgegeben werden $talt
		{
		
			$queries = array();
			// $queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$talt";
			// $queries[] = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tTid";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$calt";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$cneu";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasoniD) VALUES ($calt,2,$datetime,'Teamtausch',$season)";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasoniD) VALUES ($cneu,2,$datetime,'Teamtausch',$season)";
			
			$a = 		"
							<td>
								<center>Sie müssen folgendes Team abgeben: $taltn</center>
							</td>
						</tr>
						<tr>
							<td>
								<center>Wollen sie dieses Team wirklich für ihr neues Team $tTn abgeben?</center>
							</td>
						</tr>
						<tr>
							<td>
								<form action='boerse_v2.php' method='post'>
								<input type='text' hidden name='query' value='$q'>
								<input type='text' hidden name='teamab' value='$tabid'>
								<input type='text' hidden name='teamneu' value='$tneu'>
								<input type='text' hidden name='calt' value='$calt'>
								<input type='text' hidden name='cneu' value='$cneu'>
								<input type='text' hidden name='type' value='1'>
								<input type='text' hidden name='season' value='$season'>
								<input type='text' hidden name='uneu' value='$uneu'>
								<input type='submit' class='button eintragen' value='Ja'>
							</td>
						<td>
							<a href='javascript:window.history.back()' class='button zurueck'>Nein</a>
						</td>
						
						</form>
					";
					$q=$queries;
					$_SESSION['tquery']=$q;

		}
		break;
		
		
		case (2): //Team1 $ Team2 müssen abgegeben werden $t1id bzw. $talt & $t2id
		{
			$queries = array();
			$queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$t1id";
			$queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$t2id";
			$queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$tneu";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$t1c";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$t2c";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$cneu";
			if ($t1c==$calt)
			{
				$grund = "Teamtausch";
			}
			else
			{
				$grund = "Pflichtabgabe wegen Tausch";
			}
			if ($t2c==$calt)
			{
				$grund = "Teamtausch";
			}
			else
			{
				$grund = "Pflichtabgabe wegen Tausch";
			}
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($t1c,2,$datetime,'$grund',$season)";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($t2c,2,$datetime,'$grund',$season)";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($cneu,2,$datetime,'Teamtausch',$season)";
			
			// $a = 		"Sie müssen folgende Teams abgeben: $t1n und $t2n <br>
						// Wollen sie diese Teams wirklich für ihr neues Team $tTn abgeben?
						// <form action='teamtausch_v2.php' method='post'>
						// <input type='text' hidden name='query' value='$q'>
						// <input type='text' hidden name='team' value='$tTid'>
						// <input type='text' hidden name='type' value='1'>
						// <input type='text' hidden name='season' value='$season'>
						// <input type='submit' value='ja'>
						// </form>
									
					// ";
					
					$a = 		"
							<td>
								<center>Sie müssen folgende Teams abgeben: $t1n und $t2n</center>
							</td>
						</tr>
						<tr>
							<td>
								<center>Wollen sie diese Teams wirklich für ihr neues Team $tTn abgeben?</center>
							</td>
						</tr>
						<tr>
							<td>
								<form action='boerse_v2.php' method='post'>
								<input type='text' hidden name='query' value='$q'>
								<input type='text' hidden name='team' value='$tabid'>
								<input type='text' hidden name='team2' value='$tab2id'>
								<input type='text' hidden name='teamneu' value='$talt'>
								<input type='text' hidden name='teamtausch' value='$tneu'>
								<input type='text' hidden name='uneu' value='$uneu'>
								<input type='text' hidden name='calt' value='$t1c'>
								<input type='text' hidden name='calt2' value='$t2c'>
								<input type='text' hidden name='cneu' value='$cneu'>
								<input type='text' hidden name='type' value='2'>
								<input type='text' hidden name='season' value='$season'>
								<input type='submit' class='button eintragen' value='Ja'>
							</td>
						<td>
							<a href='javascript:window.history.back()' class='button zurueck'>Nein</a>
						</td>
						
						</form>
					";
			$q=$queries;
			$_SESSION['tquery']=$q;
			
		}
		break;
	}
	
	echo	"	<table>
					<thead id='tabelle'>
						<tr>
							<th colspan='2'></th>
						</tr>
					</thead>
					<tr>
						$a
					</tr>
					<thead id='tabelle'>
						<tr>
							<th colspan='2'></th>
						</tr>
					</thead>
				</table>";
	
	
	// echo "deb:$deb";
}
?>

</body>
</html>